import action from "./index";
import { userlist, adduser } from "../interface/user";//链接接口的方法


const initUserList = (payload: any) => {
    return {
        type: action.INITUSERLIST,
        payload
    }
}

const asyncInitUserList = (params: any) => async (dispatch: any) => {
    let res = await userlist(params);
    dispatch(initUserList(res.data.list));
}

// const asyncInitUserList =  (params: any) => {

//     return  async (dispatch:any)=>{
//         let  res  = await  userlist(params);
//         dispatch(initUserList(res.data.list))
//     }

//     // let res = await userlist(params);
//     // console.log(res.data.list);

//     // initUserList(res.data.list);
// }


let delUser = (payload: any) => {
    return {
        type: action.DELETEUSER,
        payload
    }
}

let addUser = (payload: any) => {
    return {
        type: action.ADDUSERITEM,
        payload
    }
}
let asyncAddUser = (payload: any) =>  (dispatch: any) => new Promise(async (resolve) => {

      let res = await adduser(payload);//payload是一个对象
    if (res.data.code === 200) {
        dispatch(addUser(res.data.list[0]));
        resolve({
            code:200,
            msg:"成功"
        })
    } else {
       resolve({
           code:500,
           msg:res.data.msg
       })
    }


})








export {
    delUser,
    addUser,
    initUserList,
    asyncInitUserList,
    asyncAddUser
}